package com.hzncc.scaffold.service.impl;

import cn.hutool.core.date.DateUtil;
import com.hzncc.scaffold.core.ResultT;
import com.hzncc.scaffold.entity.Other.Lecture;
import com.hzncc.scaffold.mapper.LectureMapper;
import com.hzncc.scaffold.service.LectureService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 名师讲堂表 服务实现类
 * </p>
 *
 * @author DrunkBear
 * @since 2022-04-27
 */
@Service
public class LectureServiceImpl extends ServiceImpl<LectureMapper, Lecture> implements LectureService {

    @Autowired
    LectureService lectureService;

    @Override
    public ResultT saveLecture(Lecture lecture) {
        boolean save = lectureService.save(lecture);
        if (save) {
            return ResultT.success("新增成功");
        } else {
            return ResultT.fail("新增失败");
        }
    }


    @Override
    public ResultT updateLecture(Lecture lecture) {
        boolean update = lectureService.updateById(lecture);
        if (update) {
            return ResultT.success("修改成功");
        } else {
            return ResultT.fail("修改失败");
        }
    }

    @Override
    public ResultT stayAtTop(Integer lectureId, Integer stayAtTop) {
        if (lectureService.getById(lectureId) != null) {
            Lecture lecture = lectureService.getById(lectureId);
            if (stayAtTop == 0 || stayAtTop == 1) {
                lecture.setStayAtTop(stayAtTop);
                lecture.setSortTime(DateUtil.now());
            } else {
                return ResultT.fail("数据错误");
            }
            boolean update = lectureService.updateById(lecture);
            if (update) {
                if (stayAtTop == 1) {
                    return ResultT.success("置顶成功");
                } else {
                    return ResultT.success("取消置顶成功");
                }
            } else {
                return ResultT.fail("置顶失败");
            }
        } else {
            return ResultT.fail("数据错误");
        }
    }
}
